﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace FySystem.AssistantServices
{
    public class AssistantServiceContext : IAssistantServiceContext
    {
        private Dictionary<string, IAssistantService> _services = new Dictionary<string, IAssistantService>();
        private static AssistantServiceContext _context = null;
        private static object _lock = new object();

        /// <summary>
        /// 创建服务器配置上下文，日志永不过期
        /// </summary>
        private AssistantServiceContext(bool showInConsole)
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "Logs\\";
            FyLogContext.InitLog(showInConsole, logPath);
        }

        /// <summary>
        /// 创建服务配置上下文
        /// </summary>
        /// <param name="logExpireDate">日志过期时间，单位：天</param>
        private AssistantServiceContext(int logExpireDate, bool showInConsole)
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "Logs\\";
            FyLogContext.InitLog(showInConsole, logPath);
            FyLogContext.ExpiredDate = logExpireDate;
        }

        /// <summary>
        /// 创建上下文
        /// </summary>
        /// <param name="logExpireDate"></param>
        /// <returns></returns>
        public static AssistantServiceContext CreateService(int logExpireDate, bool showInConsole)
        {
            lock(_lock)
            {
                if (_context == null)
                {
                    if (logExpireDate <= 0)
                        _context = new AssistantServiceContext(showInConsole);
                    else
                        _context = new AssistantServiceContext(logExpireDate, showInConsole);
                }
            }
            return _context;
        }

        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="service"></param>
        public IAssistantService AddService<T>(string key)
        {
            IAssistantService res = default;
            try
            {
                Type type = typeof(T);

                Type baseType = type.GetInterface("IAssistantService");                                           //获取父级类
                if (baseType != typeof(IAssistantService))
                    throw new Exception("服务未继承IAssistantService");

                res = (IAssistantService)Activator.CreateInstance(type);
                _services.Add(key, res);
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// 执行所有添加的服务
        /// </summary>
        public void Run()
        {
            try
            {
                foreach (IAssistantService service in _services.Values)
                {
                    service.InvokeConfigure();
                    service.Execute(this, service);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取已经添加的服务实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetService<T>(string key)
        {
            T res = default;
            try
            {
                if (_services.ContainsKey(key))
                {
                    res = (T)_services[key];
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
